package com.sls.game.parser.nutsnet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 
 * @description 对xml格式字符串中的中文进行指定方式的编码
 * @author Wangxuegang
 * @date 2012-9-8
 * 
 */
public class EncodeChineseCode {
	/**
	 * 对xml字符串中出现的中文进行GBK编码
	 * 
	 * @param xmlStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String encodeChineseCodeOfGBK(String xmlStr)
			throws UnsupportedEncodingException {
		StringBuilder strBu = new StringBuilder();
		if (!("".equals(xmlStr)) && xmlStr != null) {
			char[] charArr = xmlStr.toCharArray();
			for (int i = 0; i < charArr.length; i++) {
				char ch = charArr[i];
				if ((int) ch > 255) {
					String str = ch + "";
					str = URLEncoder.encode(str, "GBK");
					strBu.append(str);
				} else {
					strBu.append(ch);
				}
			}
		}

		return strBu.toString();
	}

	/**
	 * 对xml字符串中出现的中文进行GBK编码
	 * 
	 * @param xmlStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String encodeChineseCodeOfGB2312(String xmlStr)
			throws UnsupportedEncodingException {
		StringBuilder strBu = new StringBuilder();
		if (!("".equals(xmlStr)) && xmlStr != null) {
			char[] charArr = xmlStr.toCharArray();
			for (int i = 0; i < charArr.length; i++) {
				char ch = charArr[i];
				if ((int) ch > 255) {
					String str = ch + "";
					str = URLEncoder.encode(str, "GB2312");
					strBu.append(str);
				} else {
					strBu.append(ch);
				}
			}
		}

		return strBu.toString();

	}

}
